//
//  EPMSuperMarketViewController.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/4/26.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMSuperMarketViewController.h"
#import "EPMUnloginAddressView.h"
#import "EPMLeftTableView.h"
#import "EPMNaviTitleView.h"
#import "EPMRightTableView.h"
#import "EPMTopView.h"
#import "EPMTitleView.h"


@interface EPMSuperMarketViewController ()<UINavigationControllerDelegate,CAAnimationDelegate>
@property (weak,nonatomic)  EPMLeftTableView* leftTableView;
@property (weak,nonatomic)  EPMRightTableView* rightTableView;

@property (strong,nonatomic) MASConstraint* rightTableViewTop;
@property (weak,nonatomic)  UIView* titleView;

//临时存储添加的商品
@property (strong,nonatomic) EPMGoodsModel* tempModel;


@end

@implementation EPMSuperMarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.delegate = self;
    [self setNaviBarView];
    
        [self setupUI];
    
}

- (void)viewWillAppear:(BOOL)animated{
     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
   
}

#pragma mark - 默认操作 -

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}
#pragma mark - 设置UI -

- (void)setupUI{
    
    //添加左视图
    EPMLeftTableView* leftTableView = [[EPMLeftTableView alloc]init];
    self.leftTableView = leftTableView;
    
    [self.view addSubview:leftTableView];
    
    [leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
        make.width.mas_equalTo(ScreenWidth*1/4);
    }];
    //添加分类视图
    
    EPMTopView* topView = [[EPMTopView alloc]init];
    
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(leftTableView.mas_trailing);
        make.top.equalTo(leftTableView);
        make.trailing.equalTo(self.view);
    }];
    
    
    //添加titleView
    EPMTitleView* titleView = [[EPMTitleView alloc]init];
    [self.view insertSubview:titleView aboveSubview:topView];
    titleView.hidden = YES;
    self.titleView = titleView;
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(leftTableView.mas_trailing);
        make.top.equalTo(leftTableView);
        make.trailing.equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    
   
    
    
    
    //添加右视图
    
    EPMRightTableView* rightTableView = [[EPMRightTableView alloc]init];
    self.rightTableView = rightTableView;
    [self.view addSubview:rightTableView];
    [rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(leftTableView.mas_trailing);
      self.rightTableViewTop =  make.top.equalTo(topView.mas_bottom);
        make.trailing.bottom.equalTo(self.view);
    }];
    [rightTableView setRightTableViewPulling:^(BOOL isUp) {
        if(isUp){
            if (titleView.hidden == YES){
            [UIView animateWithDuration:1.0 animations:^{
              [topView mas_updateConstraints:^(MASConstraintMaker *make) {
                  make.top.equalTo(leftTableView).offset(-70);
              }];
               
                
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:1.0 animations:^{
                    titleView.hidden = NO;
                    [self.rightTableViewTop deactivate];
                    [self.rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
                        self.rightTableViewTop =  make.top.equalTo(titleView.mas_bottom);
                    }];
                    topView.hidden = YES;
                }];
                
            }];
            }
            
        }else{
            if (topView.hidden == YES){
            [UIView animateWithDuration:1.0 animations:^{
                [topView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(leftTableView);
                }];
            } completion:^(BOOL finished) {
               [UIView animateWithDuration:1.0 animations:^{
                   topView.hidden = NO;
                   [self.rightTableViewTop deactivate];
                   [self.rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
                       self.rightTableViewTop =  make.top.equalTo(topView.mas_bottom);
                   }];
                   titleView.hidden = YES;
               }];
                
            }];
        }
        }
        
    }];
    
    
    [titleView setTitleViewClicked:^{
        [UIView animateWithDuration:0.5 animations:^{
            topView.hidden = NO;
            [self.rightTableViewTop deactivate];
            [self.rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
                self.rightTableViewTop =  make.top.equalTo(topView.mas_bottom);
            }];
            
        } completion:^(BOOL finished) {
            self.titleView.hidden = YES;
        }];

    }];
    
    //默认滚到
    [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedLeftRow inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    [self.leftTableView tableView:self.leftTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedLeftRow inSection:0]];
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(marketIncreseGoodNotification:) name:EPMMarketIncreaseGoodNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homeCatergorySelectNotification:) name:EPMHomeCatergorySelectNotification object:nil];
   
}

- (void)homeCatergorySelectNotification:(NSNotification*)noti{
    NSInteger index = [noti.userInfo[KHomeSelectCatergory] integerValue];
    
    [self.leftTableView tableView:self.leftTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    
}


#pragma mark - 通知方法 动画-
- (void)marketIncreseGoodNotification:(NSNotification*)noti{
    self.tempModel = noti.userInfo[KIncreaseGoodsModel];
    //获取其实点
    CGPoint startPoint = [noti.userInfo[KIncreaseGoodPoint] CGPointValue];
    // 通过起始点计算 控制点
    CGPoint controlPoint = CGPointMake(startPoint.x + 100, startPoint.y - 50);
    // 终点
    CGPoint endPoint = CGPointMake(ScreenWidth - 150, ScreenHeight - 50);
    UIImage* img = noti.userInfo[KIncreaseGoodImage];
    
    
    UIImageView* imgView = [[UIImageView alloc]initWithImage:img];
    imgView.center = startPoint;
    
    [kWindow addSubview:imgView];
   
    // 创建抛物线路径
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    
    [bezierPath moveToPoint:startPoint];
    [bezierPath addQuadCurveToPoint:endPoint controlPoint:controlPoint];
    
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    // 设置路径
    keyFrame.path = bezierPath.CGPath;

    
    CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    //x，y轴缩小到0.1,Z 轴不变
    scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
    
    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnim.fromValue = @(1.0);
    opacityAnim.toValue = @(0.5);
    
    
    //关键帧，旋转，透明度组合起来执行
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.animations = [NSArray arrayWithObjects:keyFrame,scaleAnim,opacityAnim, nil];
    animGroup.duration = 1;
      animGroup.delegate = self;
    animGroup.removedOnCompletion = NO;
    animGroup.fillMode = kCAFillModeBoth;
    [animGroup setValue:imgView forKey:@"redBall"];
    [imgView.layer addAnimation:animGroup forKey:nil];
    
 
    
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    UIImageView *redBall = [anim valueForKey:@"redBall"];
    [redBall removeFromSuperview];
    [[NSNotificationCenter defaultCenter]postNotificationName:EPMSuperIncreaseGoodsNotification object:self userInfo:@{KIncreaseGoodsModel:self.tempModel}];
}


#pragma mark - 设置导航控制栏 -

- (void)setNaviBarView{
    EPMNaviTitleView* coverView = [[EPMNaviTitleView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    [coverView setDeafultColor];
    //添加控制器
    coverView.controller = self;
    [self.view insertSubview:coverView aboveSubview:self.view];
}



#pragma mark - UINavigationControllerDelegate代理方法 -

- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 如果进入的是当前视图控制器
    if (viewController == self) {
        
        self.navigationController.navigationBar.hidden = YES;
    } else {
        // 进入其他视图控制器
        self.navigationController.navigationBar.alpha = 1;
        // 背景颜色设置为系统默认颜色
        self.navigationController.navigationBar.tintColor = nil;
        self.navigationController.navigationBar.translucent = NO;
        
    }
    
}


#pragma mark - 移除通知 -

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


@end
